{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 引入模板\n",
    "import requests\n",
    "import json\n",
    "import pandas as pd\n",
    "from pandas.io.json import json_normalize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 百度AI开放平台\n",
    "import requests\n",
    "import json\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.127b73fce52111632fa374dbbeb32152.315360000.1926773621.282335-23580603', 'expires_in': 2592000, 'session_key': '9mzdWEHn8Jxv2cBJdNWaz5rNwpTJAG2uDsGZiCLCvzErdUwOBWv2TwHTn7u0WnbwQYJbDefUE+ImeZBVNx5+9VvFAJodxw==', 'access_token': '24.c12c503cb7a69c2996cb8f40a3debd1c.2592000.1614005621.282335-23580603', 'scope': 'public brain_all_scope brain_object_detect brain_realtime_product vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT vis-faceverify_face_beauty vis-faceverify_face_feature_sdk wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin', 'session_secret': '39132f264f29a68e0367ea56ec67c9d7'}\n"
     ]
    }
   ],
   "source": [
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=WHE2vtyulHk4D765YqVKDUlU&client_secret=utNLcgrPFYbiFxhz4jvwQch8lWWDPQvD'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 2505256515250, 'timestamp': 1611414002, 'cached': 0, 'result': {'face_num': 1, 'face_list': [{'face_token': '1f181b7536c544111a3f7867660e63d6', 'location': {'left': 161.26, 'top': 176.5, 'width': 195, 'height': 195, 'rotation': -6}, 'face_probability': 1, 'angle': {'yaw': -19.95, 'pitch': 8.59, 'roll': -4.1}, 'age': 23, 'gender': {'type': 'female', 'probability': 1}, 'emotion': {'type': 'neutral', 'probability': 0.9}}]}}\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "人脸检测与属性分析\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2795716922,2153942332&fm=26&gp=0.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"age,gender,emotion\\\"}\"\n",
    "\n",
    "access_token = '[24.c12c503cb7a69c2996cb8f40a3debd1c.2592000.1614005621.282335-23580603]'\n",
    "\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "\n",
    "headers = {'content-type': 'application/json'}\n",
    "\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23\n",
      "{'type': 'female', 'probability': 1}\n",
      "{'type': 'neutral', 'probability': 0.9}\n"
     ]
    }
   ],
   "source": [
    "print(response.json()[\"result\"][\"face_list\"][0][\"age\"])\n",
    "print(response.json()[\"result\"][\"face_list\"][0][\"gender\"])\n",
    "print(response.json()[\"result\"][\"face_list\"][0][\"emotion\"])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
